package level.editor.tool.tile;

import image.ImageUtility;

import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import core.Constants;

public class ToolTileSelect extends JPanel
{
	private ToolTile tool;

	public ToolTileSelect(ToolTile tool)
	{
		super(new FlowLayout(FlowLayout.LEADING, 0, 0));
		this.tool = tool;
	}

	public void updateTiles(String fileName)
	{
		removeAll();
		Image icon = new ImageIcon(fileName).getImage();
		int cols = (int) (icon.getWidth(null) / Constants.TILE_SIZE);
		int rows = (int) (icon.getHeight(null) / Constants.TILE_SIZE);
		BufferedImage images[] = ImageUtility.splitImage(ImageUtility
				.toBufferedImage(new ImageIcon(fileName).getImage()), cols,
				rows);

		if (images != null)
		{
			for (int i = 0; i < images.length; i++)
			{
				ToolTileButton button = new ToolTileButton(new ImageIcon(
						images[i]));
				button.setIconPath(fileName);
				button.setSx((int) ImageUtility.extractPoint(cols, rows, i,
						(int) Constants.TILE_SIZE).x);
				button.setSy((int) ImageUtility.extractPoint(cols, rows, i,
						(int) Constants.TILE_SIZE).y);
				button.setActionCommand("Tile");
				button.addActionListener(tool);
				add(button);
			}
		}
		revalidate();
	}

}
